package testcase;

import connectionpool.C3P0Executor;
import connectionpool.DruidExecutor;
import connectionpool.HikariExecutor;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class CheckParamsWithHikari {

    private Logger LOGGER = Logger.getLogger(CheckParamsWithHikari.class);

    private static C3P0Executor c3P0Executor;

    private static DruidExecutor druidExecutor;

    private static HikariExecutor hikariExecutor;

    private static String urlPrefix = "jdbc:opengauss://192.168.0.243:14100,192.168.0.243:14200,192.168.0" +
        ".243:14300/test?";

    private static String username = "test";

    private static String password = "huawei123.";

    private static int num = 60;

    private static void getHikariExecutor(HikariExecutor executor) throws InterruptedException, SQLException {
        List<Connection> connections = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            Connection connection = executor.getConnection();
            connections.add(connection);
            Thread.sleep(1000);
        }
        for (Connection connection : connections) {
            connection.close();
        }
    }
    @Test
    public void setEnableQuickAutoBalanceFALseWithC3P0Test() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&enableQuickAutoBalance=FALse";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setEnableQuickAutoBalanceTRueWithC3P0Test() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&enableQuickAutoBalance=TRue";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setEnableQuickAutoBalanceTrueWithC3P0Test() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&enableQuickAutoBalance=true";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setEnableQuickAutoBalanceFalseWithC3P0Test() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&enableQuickAutoBalance=false";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMaxIdleTimeBeforeTerminalTooSmallWithC3P0Test() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&maxIdleTimeBeforeTerminal=-1";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMaxIdleTimeBeforeTerminalTooBigWithC3P0Test() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&maxIdleTimeBeforeTerminal=9223372036854775";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMaxIdleTimeBeforeTerminalParsedFailedTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&maxIdleTimeBeforeTerminal=1021sfsf1";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMaxIdleTimeBeforeTerminalParsedSuccessTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&maxIdleTimeBeforeTerminal=9223372036854774";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerClusterTooSmallTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerCluster=-1";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerClusterTooBigTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerCluster=101";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerClusterParsedFailedTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerCluster=101sfsfs";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerClusterParsedSuccessTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerCluster=0";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerDatanodeTooSmallTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerDatanode=-1";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerDatanodeTooBigTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerDatanode=101";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerDatanodeParsedFailedTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerDatanode=101sfsfs";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }

    @Test
    public void setMinReservedConPerDatanodeParsedSuccessTest() throws SQLException, InterruptedException {
        String subString = "autoBalance=leastconn&minReservedConPerDatanode=0";
        System.out.println(urlPrefix + subString);
        hikariExecutor = new HikariExecutor(urlPrefix + subString, username, password, num, num);
        getHikariExecutor(hikariExecutor);
        hikariExecutor.close();
    }
}